package com.juc.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Author Zan
 * @Create 2024/5/6 22:33
 * @ClassName: CompletableFutureAPIDemo
 * @Description : TODO 请用一句话描述该类的功能
 */
public class CompletableFutureAPI5Demo {
    public static void main(String[] args) {
        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" + "----启动");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 10;
        });

        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" + "----启动");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 5;
        });

        CompletableFuture<Integer> completableFuture = completableFuture1.thenCombine(completableFuture2, (a, b) -> {
            System.out.println("-------开始两个结果合并");
            return a + b;
        });
        System.out.println(completableFuture.join());
    }
}
